package com.example.yuejianapp14.Activity.Model;

import com.example.yuejianapp14.Activity.JavaBean.Collected;
import com.example.yuejianapp14.Activity.JavaBean.Success;
import com.example.yuejianapp14.Activity.Listener.CollectedListener;
import com.example.yuejianapp14.Activity.Listener.ListListener;
import com.example.yuejianapp14.Activity.Servics.CollectedService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by jason_lin on 2017/10/31.
 */

public class CollectedModel {
    private Retrofit retrofit;
    private String BASEURL ="http://10.0.2.2/YueJian/index.php/Api/Collected/";
    private String HeadPhotoNameDefault="user_headphoto.png";

    public CollectedModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    public void Collect(String uid, String type, String typeid, final ListListener<Success> listener){
        CollectedService collectedService=retrofit.create(CollectedService.class);
        Call<Success> call=collectedService.collect( uid,type,typeid);
        call.enqueue(new Callback<Success>() {
            @Override
            public void onResponse(Call<Success> call, Response<Success> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else
                    listener.onFail("onresponse fail");
            }
            @Override
            public void onFailure(Call<Success> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
    public void CollectInfo(String uid, String type, String typeid, final ListListener<Success> listener){
        CollectedService collectedService=retrofit.create(CollectedService.class);
        Call<Success> call=collectedService.collectinfo( uid,type,typeid);
        call.enqueue(new Callback<Success>() {
            @Override
            public void onResponse(Call<Success> call, Response<Success> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else
                    listener.onFail("onresponse fail");
            }
            @Override
            public void onFailure(Call<Success> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
    public void findCollected(String uid,final CollectedListener listener) {
        CollectedService collectedService=retrofit.create(CollectedService.class);
        Call<List<Collected>> call=collectedService.findcollected(uid);
        call.enqueue(new Callback<List<Collected>>() {
            @Override
            public void onResponse(Call<List<Collected>> call, Response<List<Collected>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else
                    listener.onFail("onresponse fail");
            }


            @Override
            public void onFailure(Call<List<Collected>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
